package academic.concordia.bankofquebec.bean.payment;

import org.springframework.transaction.annotation.Transactional;

import academic.concordia.bankofquebec.dao.bankaccount.IBankAccountDAO;
import academic.concordia.bankofquebec.dao.client.IClientDAO;
import academic.concordia.bankofquebec.dao.payment.IPaymentDAO;
import academic.concordia.bankofquebec.entity.BankAccount;
import academic.concordia.bankofquebec.entity.Payment;
import academic.concordia.bankofquebec.entity.Transaction;
import academic.concordia.bankofquebec.entity.TransactionStatus;

@Transactional(readOnly = true)
public class PaymentBean implements IPaymentBean {
	
	IPaymentDAO paymentDAO;
	
	IBankAccountDAO bankAccountDAO;
	
	IClientDAO clientDAO;
	
	@Override
	@Transactional(readOnly = false)
	public Transaction makePayment(Payment payment, String accountId) {
		
		BankAccount account = bankAccountDAO.getAccountById(accountId);
		Transaction paymentTrx = account.payment(payment.getValue());
		if (paymentTrx.isApproved()){
			payment.setAccount(account);
			paymentDAO.addPayment(payment);
			bankAccountDAO.updateBankAccount(account);
		}
		return paymentTrx;
	}

	public IPaymentDAO getPaymentDAO() {
		return paymentDAO;
	}

	public void setPaymentDAO(IPaymentDAO paymentDAO) {
		this.paymentDAO = paymentDAO;
	}

	public IBankAccountDAO getBankAccountDAO() {
		return bankAccountDAO;
	}

	public void setBankAccountDAO(IBankAccountDAO bankAccountDAO) {
		this.bankAccountDAO = bankAccountDAO;
	}

	public IClientDAO getClientDAO() {
		return clientDAO;
	}

	public void setClientDAO(IClientDAO clientDAO) {
		this.clientDAO = clientDAO;
	}
}
